import { ref, reactive } from 'vue'
import ps from '@/api/ps.js'
import { addComma } from '@/utils/h_utils.js'

export default function () {
  const loading = ref(false)
  const maxClientNums = ref()
  const currentClientNums = ref()
  const data = ref([])
  const annualSalesPlanList = ref([])
  const commissionRate = reactive({
    branch: '',
    currency: ''
  })
  const commissionRateType = ref()
  const cYearsPercents = ref([])
  const cSalesAmountPercents = ref([])
  const cYearQuotas = ref([])

  function getData() {
    loading.value = true
    return ps.getPersonalSettings().then(res => {
      if (res.code === 2000) {
        maxClientNums.value = res.data.maxOfTaget.maxClientNums || 0
        currentClientNums.value = res.data.maxOfTaget.currentClientNums || 0
      }
      annualSalesPlanList.value = res.data.salesPlans
      const num = res.data.weeklyTasks.length / 7
      const arr = []
      for (let i = 0; i < num; i++) {
        const obj = {}
        obj.buName = res.data.weeklyTasks[7 * i].buName
        obj.touchedClients = res.data.weeklyTasks[7 * i].weekStandard
        obj.responsiveClients = res.data.weeklyTasks[7 * i + 1].weekStandard
        obj.positiveClients = res.data.weeklyTasks[7 * i + 2].weekStandard
        obj.pThree = res.data.weeklyTasks[7 * i + 3].weekStandard
        obj.pTwo = res.data.weeklyTasks[7 * i + 4].weekStandard
        obj.pOne = res.data.weeklyTasks[7 * i + 5].weekStandard
        obj.closeClients = res.data.weeklyTasks[7 * i + 6].weekStandard
        arr.push(obj)
      }
      data.value = arr

      commissionRate.branch = res.data.commissonRate.branchName
      commissionRate.currency = res.data.commissonRate.currencyName
      if (res.data.commissonRate.performSale) {
        commissionRateType.value = res.data.commissonRate.performSale.calculationType
      }
      if (res.data.commissonRate.yearPercents.length > 0) {
        cYearsPercents.value = res.data.commissonRate.yearPercents.map(item => {
          const obj = {}
          obj.range = item.minYear + ' ~ ' + item.maxYear
          obj.per = (item.yearPercent * 100).toFixed(2) + '%'
          return obj
        })
      }
      if (res.data.commissonRate.salesAmountPercents.length > 0) {
        cSalesAmountPercents.value = res.data.commissonRate.salesAmountPercents.map(item => {
          const obj = {}
          obj.range =
            addComma(String(item.minPrice / 1000)) + 'K' + ' ~ ' + addComma(String(item.maxPrice / 1000)) + 'K'
          obj.per = (item.moneyPercent * 100).toFixed(2) + '%'
          return obj
        })
      }
      if (res.data.commissonRate.yearQuotas.length > 0) {
        cYearQuotas.value = res.data.commissonRate.yearQuotas
      }
    })
  }

  function getMonth(val) {
    let month = ''
    switch (val) {
      case 1:
        month = 'Jan'
        break
      case 2:
        month = 'Feb'
        break
      case 3:
        month = 'Mar'
        break
      case 4:
        month = 'Apr'
        break
      case 5:
        month = 'May'
        break
      case 6:
        month = 'Jun'
        break
      case 7:
        month = 'Jul'
        break
      case 8:
        month = 'Aug'
        break
      case 9:
        month = 'Sep'
        break
      case 10:
        month = 'Oct'
        break
      case 11:
        month = 'Nov'
        break
      case 12:
        month = 'Dec'
        break
    }
    return month
  }

  return {
    getData,
    getMonth,
    loading,
    maxClientNums,
    currentClientNums,
    annualSalesPlanList,
    data,
    commissionRate,
    commissionRateType,
    cYearsPercents,
    cSalesAmountPercents,
    cYearQuotas
  }
}
